package com.nulldev.util.os.elvation;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.FinalBypass;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.Theme;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.os.OSUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;

public class ElevationUI extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(final String command, final String softwareName, final boolean failedTry, final Eleavtion event) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ElevationUI frame = new ElevationUI(command, softwareName, failedTry, event);
					frame.setVisible(true);
					FontManager.loadSystemSpecificFonts();
					ThemeApplier.applyTheme(frame);
					SwingUtil.centerFrame(frame);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param event 
	 */
	public ElevationUI(final String command, final String softwareName, final boolean failedTry, final Eleavtion event) {
		final FinalBypass<Boolean> elevated = FinalBypass.createBypass(Boolean.class);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				if (elevated.getObject()) {
					return;
				}
				event.elevated(ElevationState.NO_INPUT, null);
			}
		});
		setIconImage(Toolkit.getDefaultToolkit().getImage(ElevationUI.class.getResource("/com/nulldev/util/java/swing/nullutil_logo.png")));
		setTitle("Elevation Required");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 494, 226);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblsoftwareRequiresElevation = new JLabel(softwareName + " requires elevation to continue.");
		
		passwordField = new JPasswordField();
		passwordField.setEchoChar('*');
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(ElevationUI.class.getResource("/com/nulldev/util/java/swing/extSwing/resources/warning.png")));
		
		JLabel lblEnterYourPassword = new JLabel("Enter your password to continue:");
		
		JLabel lblCommandcommand = new JLabel("Command: " + command);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				elevated.setObject(true);
				event.elevated(ElevationState.INPUT_RECIEVED, new String(passwordField.getPassword()));
				ElevationUI.this.setVisible(false);
				ElevationUI.this.dispose();
			}
		});
		
		JLabel lblOrUseYour = new JLabel("Or use your fingerprint.");
		lblOrUseYour.setVisible((OSUtil.isLinux() && OSUtil.PackageUtil.isPackage("fingerprint-identifier")));
		
		JLabel lblTryAgain = new JLabel("Try again.") {
			@Override
			public void setForeground(final Color color) {
				super.setForeground(Color.orange);
			}
		};
		lblTryAgain.setVisible(failedTry);
		lblTryAgain.setForeground(Color.ORANGE);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(label)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(19)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblEnterYourPassword)
								.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
								.addComponent(lblsoftwareRequiresElevation)
								.addComponent(lblOrUseYour)
								.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
									.addComponent(lblTryAgain)
									.addPreferredGap(ComponentPlacement.RELATED, 288, Short.MAX_VALUE)
									.addComponent(btnOk))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addComponent(lblCommandcommand)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblsoftwareRequiresElevation)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblCommandcommand))
						.addComponent(label))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblEnterYourPassword)
					.addGap(16)
					.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblOrUseYour)
					.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOk)
						.addComponent(lblTryAgain))
					.addGap(22))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
